package org.mall.modules.order.controller.users;

import io.swagger.annotations.ApiOperation;
import org.mall.common.api.vo.CommonResult;
import org.mall.common.constant.DictKeyConstants;
import org.mall.convert.order.application.OrderReturnConvert;
import org.mall.modules.order.service.OrderReturnService;
import org.mall.modules.pojo.order.bo.OrderReturnInfoBO;
import org.mall.modules.pojo.order.dto.OrderReturnApplyDTO;
import org.mall.modules.pojo.order.po.user.OrderReturnApplyPO;
import org.mall.modules.system.entity.DataDictBO;
import org.mall.modules.system.service.DataDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单退款/售后流程
 *
 * @author Sin
 * @time 2019-04-25 22:04
 */
@RestController
@RequestMapping("/users/orderReturn/")
public class OrderReturnController {

    @Autowired
    private OrderReturnService orderReturnService;

    @Autowired
    private DataDictService dataDictService;

    @GetMapping("reason")
    @ApiOperation("原因")
    public CommonResult<List<DataDictBO>> orderReturnReason() {
        return dataDictService.getDataDict(DictKeyConstants.ORDER_RETURN_REASON);
    }

    @PostMapping("apply")
    @ApiOperation("订单售后")
    public CommonResult orderReturnApply(@RequestBody OrderReturnApplyPO orderReturnApplyPO) {
        OrderReturnApplyDTO applyDTO = OrderReturnConvert.INSTANCE.convert(orderReturnApplyPO);
        return orderReturnService.orderReturnApply(applyDTO);
    }

    @GetMapping("info")
    @ApiOperation("订单售后详细")
    public CommonResult<OrderReturnInfoBO> orderApplyInfo(@RequestParam("orderId") Long orderId) {
        CommonResult<OrderReturnInfoBO> commonResult = orderReturnService.orderApplyInfo(orderId);

        // 转换 字典值
        if (commonResult.isSuccess()) {
            CommonResult<DataDictBO> dataDictResult = dataDictService.getDataDict(
                    DictKeyConstants.ORDER_RETURN_SERVICE_TYPE,
                    commonResult.getData().getReturnInfo().getServiceType());

            if (dataDictResult.isSuccess()) {
                commonResult.getData().getReturnInfo().setServiceTypeText(dataDictResult.getData().getDisplayName());
            }
        }

        return commonResult;
    }
}
